//
//  FPMyWebCollectController.m
//  FilmParadise
//
//  Created by lanou3g on 15/7/24.
//  Copyright (c) 2015年 DLYgroup. All rights reserved.
//

#import "FPMyWebCollectController.h"
#import "FPSqliteManager.h"
@interface FPMyWebCollectController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *WebArray;
@end

@implementation FPMyWebCollectController
- (NSMutableArray *)WebArray
{
    if (!_WebArray) {
        _WebArray = [NSMutableArray array];
    }
    return _WebArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"cgClear"] forBarMetrics:UIBarMetricsDefault];
    UIView *statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth , 20)];
    statusBarView.backgroundColor = [UIColor colorWithRed:0.180 green:0.596 blue:1.000 alpha:1.000];
    [self.view addSubview:statusBarView];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView reloadData];
    [self arrayFromDB];
}

- (void)arrayFromDB
{
    NSArray *arr = [FPSqliteManager selectWebCollectTitleWithUserName:[FPUserModel ShareInstance].userName];
    if (arr.count == 0) {
        return;
    }
    for (NSString *title in arr) {
        FPIntroModel *model =  [FPSqliteManager selectAllWebModelWithTitle:title];
        [self.WebArray addObject:model];
    }
 }
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.WebArray.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    FPIntroModel *model = self.WebArray[indexPath.row];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:nil];
    cell.textLabel.text = model.title;
    cell.detailTextLabel.text = model.title2;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    FPIntroModel *model = self.WebArray[indexPath.row];
    NSArray *titleArray = [FPSqliteManager selectWebCollectTitleWithUserName:[FPUserModel ShareInstance].userName];
    for (NSString *title in titleArray) {
        if ([title isEqualToString:model.title]) {
            if (editingStyle == UITableViewCellEditingStyleDelete) {
                [FPSqliteManager deleteWebModelWithTitle:title];
                [self.WebArray removeObject:model];
                [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:   UITableViewRowAnimationFade];
                [self.tableView reloadData];
            }
        }
    }
}
@end
